package com.zh.entity.advanced.controller;

import com.alibaba.fastjson.JSON;
import com.zh.entity.advanced.entity.DemoEntity;
import com.zh.entity.advanced.entity.DemoValidEntity;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.validation.Valid;
import java.math.BigDecimal;

@Controller
public class CheckEntityController {

    @RequestMapping("/checkparam")
    @ResponseBody
    public String CheckParam(@RequestBody DemoEntity demoEntity){
        if (demoEntity.getParamInt() != null){
            System.out.println("demoEntity.getParamInt()不能为null");
        }else if (demoEntity.getParamInt() >= 0){
            System.out.println("demoEntity.getParamInt()必须大于等于0");
        }
        if (demoEntity.getParamB() != 0 || demoEntity.getParamB() != 1){
            System.out.println("demoEntity.getParamB()不等于0或1");
        }
        if (demoEntity.getParamBb() != null && demoEntity.getParamBb().compareTo(BigDecimal.valueOf(1000)) > 0){
            System.out.println("demoEntity.getParamBb()大于1000");
        }
        if (StringUtils.isEmpty(demoEntity.getParamStr())){
            System.out.println("demoEntity.getParamStr()不能为空或null");
        }
        return JSON.toJSONString(demoEntity);
    }

    @RequestMapping("/validparam")
    @ResponseBody
    public String ValidParam(@Valid @RequestBody DemoValidEntity demoValidEntity){

        return JSON.toJSONString(demoValidEntity);
    }
}
